#include "fsxwsmainwindow.h"
#include "ui_fsxwsmainwindow.h"
using namespace FsxWsGUI;

//
// Test FSX configuration
//
void FsxWsMainWindow::onMenuFSXTest()
{
    FsxWsMainWindow::slotSetConsoleTab();
    if (FsxWsMainWindow::_fsxSimConnect && FsxWsMainWindow::_fsxSimConnect->isRunning()) {
        FsxWsMainWindow::slotLogMessage("You are already connected to FSX, nothing to do", QtDebugMsg);
        return;
    }

    FsxWsMainWindow::slotLogMessage("Will try to configure, please be patient ...", QtDebugMsg);
    ui->qtbConsole->repaint(); // at least a message before it might hang for a while

    // try to connect
    if (FsxWsMainWindow::connectFsxSimConnect()) {
        FsxWsMainWindow::slotLogMessage("Connected to FSX, all fine", QtDebugMsg);
        FsxWsMainWindow::initFsxSimConnectButtons();
        return;
    }

    // last resort
    FsxWsMainWindow::slotLogMessage("Cannot connect to FSX, is FSX started / network connected?", QtDebugMsg);
    return;
}

//
// Save a file with data as KML
//
void FsxWsMainWindow::onMenuFSXSaveKmlData(bool withReducedResolution) {
    int reducedResolutionMs = -1;
    if (withReducedResolution) {
        bool success;
        reducedResolutionMs = QInputDialog::getInteger(this, "Reduce resolution", "Waypoint resolution [ms]",
                                                       WPMINREDUCEDRESOLUTION, WPMINREDUCEDRESOLUTION * 5, WPMINREDUCEDRESOLUTION * 20, 1000, &success);
        if(!success) return;
    }
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save KML data (Google Earth)"), "",
                                                    tr("KML (*.kml);;All Files (*)"));
    if (fileName.length() >0) {
        FsxWsMainWindow::saveWaypointsAsKml(fileName, reducedResolutionMs);
    }
}

//
// Save a file with data as KML (reduced resolution)
//
void FsxWsMainWindow::onMenuFSXSaveKmlDataReduced() {
    FsxWsMainWindow::onMenuFSXSaveKmlData(true);
}

//
// Show configuration dialog for WebServer
//
void FsxWsMainWindow::onMenuWsConfigure()
{
    FsxWsMainWindow::showConfigureTab(CONFTABWS);
}

//
// Show coniguration dialog for WebServer
//
void FsxWsMainWindow::onMenuWsShowInBrowser() {
    if (!FsxWsMainWindow::_fsxWsSettings) return;
    FsxWsMainWindow::onStartWebServiceClicked();
    QUrl url(RequestDispatcher::getDocrootUrl());
    QDesktopServices::openUrl(url);
}

//
// Show the configuration dialog
//
void FsxWsMainWindow::onMenuFSXConfigure() {
    FsxWsMainWindow::showConfigureTab(CONFTABFSX);
}

//
// Show the configuration dialog
//
void FsxWsMainWindow::onMenuAppKmlConfigure() {
    FsxWsMainWindow::showConfigureTab(CONFTABKML);
}

//
// About dialog
//
void FsxWsMainWindow::onMenuHelpAbout()
{
    if (!FsxWsMainWindow::_aboutDialog) return;
    FsxWsMainWindow::_aboutDialog->show();
}

//
// Save a file with data as JSON
//
void FsxWsMainWindow::onMenuFSXSaveJsonData() {
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save JSON data"), "",
                                                    tr("JSON (*.js *.txt);;All Files (*)"));
    if (fileName.length() >0) {
        FsxWsMainWindow::saveJsonDataAsFile(fileName);
    }
}

//
// Save a SimConnect.cfg file
//
void FsxWsMainWindow::onMenuFSXCreateSimConnectCfg() {
    QString fileName = FSXHelper::getLocalSimConnectCfgFilename();
    QString ipPort = QString("IP: %1 Port: %2").arg(FsxWsMainWindow::_fsxWsSettings->getFsxIpAddress()).arg(FsxWsMainWindow::_fsxWsSettings->getFsxIpPort());
    fileName = QFileDialog::getSaveFileName(this, "Save 'SimConnect.cfg' file " + ipPort, fileName,
                                            tr("SimConnect (*.cfg);;All Files (*)"));
    if (fileName.isEmpty()) return;
    if (FSXHelper::simConnectCfg(fileName, FsxWsMainWindow::_fsxWsSettings->getFsxIpAddress(), FsxWsMainWindow::_fsxWsSettings->getFsxIpPort())) {
        FsxWsMainWindow::slotLogMessage(QString("SimConnect default file written: %1").arg(fileName), QtDebugMsg);
        FsxWsMainWindow::slotLogMessage(ipPort, QtDebugMsg);
    } else {
        FsxWsMainWindow::slotLogMessage(QString("SimConnect NOT(!) written: %1").arg(fileName), QtWarningMsg);
    }
}

//
// Delete the local SimConnect.cfg file
//
void FsxWsMainWindow::onMenuFSXDeleteSimConnectCfg()
{
    QString fileName = QCoreApplication::applicationDirPath() + "/SimConnect.cfg";
    if (QFile::remove(fileName)) {
        FsxWsMainWindow::slotLogMessage(QString("SimConnect default file deleted: %1").arg(fileName), QtDebugMsg);
    }
}

//
// Help has been called
//
void FsxWsMainWindow::onMenuHelpShowOnlineHelp() {
    QUrl url(APPURLHELP);
    QDesktopServices::openUrl(url);
}
